$(document).ready(function () {
    var date = new Date();//伤害发生日期：用户可以按照伤害发生日期进行查询，必输项，默认为当天
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    $("#tdate").val(year + "-" + month + "-" + day);

    var marks = "";//检查评论
    init();
   $("#tdate").change(   function () {
       var tb = document.getElementById('t1');
       var rowNum=tb.rows.length;
       for (i=1;i<rowNum;i++)
       {
           tb.deleteRow(i);
           rowNum=rowNum-1;
           i=i-1;
       }

           $("#tdate").attr("value", $(this).val()); //赋值
            var date = $("#tdate").val();
            $.ajax({
                type: "get",
                url: "/lookSelfExam/commentHomework",
                dataType: "json",
                data: {
                    date: date
                },
                success: function (data) {
                    $("#winnerId").empty();
                    for (var i in data) {
                        $("#winnerId").append("<option value=" + data[i].userId + ">" + data[i].userName + "</option>");
                        if (data[i].score != null && data[i].comment != null) {
                            $("#t1").append("<tr><td>" + data[i].userId + "</td>" +
                                "<td>" + data[i].userName + "</td>" +
                                "<td>" + data[i].spendTime + "</td>" +
                                "<td>" + data[i].score + "</td>" +
                                "<td >\<input type='button' class='searchHomework' value='查看'>\</td></tr>");
                        } else {
                            $("#t1").append("<tr><td>" + data[i].userId + "</td>" +
                                "<td>" + data[i].userName + "</td>" +
                                "<td>" + data[i].spendTime + "</td>" +
                                "<td>" + "未评分" + "</td>" +
                                "<td >\<input type='button' class='correctHomework' value='批改'>\</td></tr>");
                        }
                    }

                    $(".correctHomework").click(function () {
                        var userId = $(this).parent().parent().find("td").eq(0).text();
                        $(this).val("提交");
                        if ($(this).val() == "提交") {
                            $(this).click(function () {
                                var comment = $("#comment").val();
                                var date = $("#tdate").val();
                                var radios = document.getElementsByName("theRadio");
                                if (comment == null || comment == "") {
                                    alert("请填写评论");
                                    return;
                                }


                                for (i = 0; i < radios.length; i++) {
                                    if (radios[i].checked) {
                                        marks = 1;
                                        var radioValue = radios[i].value;
                                        $.ajax({
                                            type: "POST",
                                            url: "/lookSelfExam/uploadResult/",
                                            dataType: "text",
                                            data: {
                                                comment: comment,
                                                date: date,
                                                score: radioValue,
                                                userId: userId
                                            },
                                            success: function (data) {
                                                alert("提交成功");
                                                if (1 == data) {
                                                    $(this).val("查看");
                                                }
                                                window.location.href="http://m.zizai.pro/html/lookSelfExam.html";
                                                console.log(data.result);
                                            },
                                            error: function (xhr, textStatus) {
                                                console.log('错误');
                                                console.log(xhr);
                                                console.log(textStatus);
                                            }
                                        });
                                        break;
                                    }
                                }
                                if (marks != 1) {
                                    alert("请打分");
                                }
                            });
                        }

                        $.ajax({
                            type: "get",
                            url: "/lookSelfExam/searchHomework",
                            dataType: "json",
                            data: {
                                userId: userId,
                                date: date
                            },
                            success: function (data) {
                                $("#t2").empty();
                                for (var i in data.names) {
                                    $("#t2").append("<td ><input type='button' class='lookHomework' value=" + data.names[i] + ">" + "</td>");
                                }
                                var len = $("input[name='theRadio']").length;
                                for (var j = 0; j < len; j++) {
                                    if ($("input[name='theRadio']:eq(" + j + ")").val() == data.score) {
                                        $("input[name='theRadio']").get(j).checked = true;
                                    }
                                }
                                $("#comment").val(data.comment);


                                $(".lookHomework").click(function () {
                                    var name = $(this).val();
                                    $.ajax({
                                        type: "GET",
                                        url: "/lookSelfExam/fileNames/",
                                        dataType: "json",
                                        data: {
                                            name: name
                                        },
                                        success: function (data) {
                                            $("#lookHomework").val(data.result);
                                            $("#lookHomework").show();
                                            console.log(data.result);
                                        },
                                        error: function (xhr, textStatus) {
                                            console.log('错误');
                                            console.log(xhr);
                                            console.log(textStatus);
                                        }
                                    });
                                });


                                console.log(data);
                            },
                            error: function (xhr, textStatus) {
                                console.log('错误');
                                console.log(xhr);
                                console.log(textStatus);
                            }
                        });
                    });

                    $(".searchHomework").click(function () {
                        var userId = $(this).parent().parent().find("td").eq(0).text();
                        $.ajax({
                            type: "get",
                            url: "/lookSelfExam/searchHomework",
                            dataType: "json",
                            data: {
                                userId: userId,
                                date: date
                            },
                            success: function (data) {
                                $("#t2").empty();
                                for (var i in data.names) {
                                    $("#t2").append("<td ><input type='button' class='lookHomework' value=" + data.names[i] + ">" + "</td>");
                                }
                                var len = $("input[name='theRadio']").length;
                                for (var j = 0; j < len; j++) {
                                    if ($("input[name='theRadio']:eq(" + j + ")").val() == data.score) {
                                        $("input[name='theRadio']").get(j).checked = true;
                                    }
                                }
                                $("#comment").val(data.comment);
                                $(".lookHomework").click(function () {
                                    var name = $(this).val();
                                    $.ajax({
                                        type: "GET",
                                        url: "/lookSelfExam/fileNames/",
                                        dataType: "json",
                                        data: {
                                            name: name
                                        },
                                        success: function (data) {
                                            $("#lookHomework").val(data.result);
                                            $("#lookHomework").show();
                                            console.log(data.result);
                                        },
                                        error: function (xhr, textStatus) {
                                            console.log('错误');
                                            console.log(xhr);
                                            console.log(textStatus);
                                        }
                                    });
                                });
                                console.log(data);
                            },
                            error: function (xhr, textStatus) {
                                console.log('错误');
                                console.log(xhr);
                                console.log(textStatus);
                            }
                        });
                    });
                    console.log(data);
                },
                error: function (xhr, textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
        });

    function init() {
       // $("#tdate").attr("value", $(this).val()); //赋值
        var date = $("#tdate").val();
        $.ajax({
            type: "get",
            url: "/lookSelfExam/commentHomework",
            dataType: "json",
            data: {
                date: date
            },
            success: function (data) {
                $("#winnerId").empty();
                for (var i in data) {

                    $("#winnerId").append("<option value=" + data[i].userId + ">" + data[i].userName + "</option>");

                    if (data[i].score != null && data[i].comment != null) {
                        $("#t1").append("<tr><td>" + data[i].userId + "</td>" +
                            "<td>" + data[i].userName + "</td>" +
                            "<td>" + data[i].spendTime + "</td>" +
                            "<td>" + data[i].score + "</td>" +
                            "<td >\<input type='button' class='searchHomework' value='查看'>\</td></tr>");
                    } else {
                        $("#t1").append("<tr><td>" + data[i].userId + "</td>" +
                            "<td>" + data[i].userName + "</td>" +
                            "<td>" + data[i].spendTime + "</td>" +
                            "<td>" + "未评分" + "</td>" +
                            "<td >\<input type='button' class='correctHomework' value='批改'>\</td></tr>");
                    }
                }

                $(".correctHomework").click(function () {
                    var userId = $(this).parent().parent().find("td").eq(0).text();
                    $(this).val("提交");
                    if ($(this).val() == "提交") {
                        $(this).click(function () {
                            var comment = $("#comment").val();
                            var date = $("#tdate").val();
                            var radios = document.getElementsByName("theRadio");
                            if (comment == null || comment == "") {
                                alert("请填写评论");
                                return;
                            }


                            for (i = 0; i < radios.length; i++) {
                                if (radios[i].checked) {
                                    marks = 1;
                                    var radioValue = radios[i].value;
                                    $.ajax({
                                        type: "POST",
                                        url: "/lookSelfExam/uploadResult/",
                                        dataType: "text",
                                        data: {
                                            comment: comment,
                                            date: date,
                                            score: radioValue,
                                            userId: userId
                                        },
                                        success: function (data) {
                                            alert("提交成功");
                                            if (1 == data) {
                                                $(this).val("查看");
                                            }
                                            console.log(data.result);
                                        },
                                        error: function (xhr, textStatus) {
                                            console.log('错误');
                                            console.log(xhr);
                                            console.log(textStatus);
                                        }
                                    });
                                    break;
                                }
                            }
                            if (marks != 1) {
                                alert("请打分");
                            }
                        });
                    }

                    $.ajax({
                        type: "get",
                        url: "/lookSelfExam/searchHomework",
                        dataType: "json",
                        data: {
                            userId: userId,
                            date: date
                        },
                        success: function (data) {
                            $("#t2").empty();
                            for (var i in data.names) {
                                $("#t2").append("<td ><input type='button' class='lookHomework' value=" + data.names[i] + ">" + "</td>");
                            }
                            var len = $("input[name='theRadio']").length;
                            for (var j = 0; j < len; j++) {
                                if ($("input[name='theRadio']:eq(" + j + ")").val() == data.score) {
                                    $("input[name='theRadio']").get(j).checked = true;
                                }
                            }
                            $("#comment").val(data.comment);


                            $(".lookHomework").click(function () {
                                var name = $(this).val();
                                $.ajax({
                                    type: "GET",
                                    url: "/lookSelfExam/fileNames/",
                                    dataType: "json",
                                    data: {
                                        name: name
                                    },
                                    success: function (data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error: function (xhr, textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });


                            console.log(data);
                        },
                        error: function (xhr, textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });

                $(".searchHomework").click(function () {
                    var userId = $(this).parent().parent().find("td").eq(0).text();
                    $.ajax({
                        type: "get",
                        url: "/lookSelfExam/searchHomework",
                        dataType: "json",
                        data: {
                            userId: userId,
                            date: date
                        },
                        success: function (data) {
                            $("#t2").empty();
                            for (var i in data.names) {
                                $("#t2").append("<td ><input type='button' class='lookHomework' value=" + data.names[i] + ">" + "</td>");
                            }
                            var len = $("input[name='theRadio']").length;
                            for (var j = 0; j < len; j++) {
                                if ($("input[name='theRadio']:eq(" + j + ")").val() == data.score) {
                                    $("input[name='theRadio']").get(j).checked = true;
                                }
                            }
                            $("#comment").val(data.comment);
                            $(".lookHomework").click(function () {
                                var name = $(this).val();
                                $.ajax({
                                    type: "GET",
                                    url: "/lookSelfExam/fileNames/",
                                    dataType: "json",
                                    data: {
                                        name: name
                                    },
                                    success: function (data) {
                                        $("#lookHomework").val(data.result);
                                        $("#lookHomework").show();
                                        console.log(data.result);
                                    },
                                    error: function (xhr, textStatus) {
                                        console.log('错误');
                                        console.log(xhr);
                                        console.log(textStatus);
                                    }
                                });
                            });
                            console.log(data);
                        },
                        error: function (xhr, textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                });
                console.log(data);
            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }

    $("#upload1").click(function () {
        var winnerId = $("#winnerId").val();
        var date = $("#tdate").val();
        $.ajax({
            type: "POST",
            url: "/lookSelfExam/setWinnerId/",
            dataType: "text",
            data: {
                winnerId: winnerId,
                date: date
            },
            success: function (data) {
                alert("上传成功！")
                console.log(data);
            },
            error: function (xhr, textStatus) {
                alert("上传失败！请输入正确的用户id!")
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    })
});